﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;

namespace RM2012Server.Model
{
    public class XML
    {
        public static XmlDocument CreateXMLDocument(String message)
        {
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.LoadXml(message);
            }
            catch
            {
                MessageBox.Show("Tin nhắn gửi tới không đúng định dạng XML", "LỖI", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return doc;
        }

        public static XmlDocument XmlReader(String filename)
        {
            XmlDocument doc = new XmlDocument();

            try
            {
                doc.Load(filename);
            }
            catch
            {
                MessageBox.Show("Không đọc được hoặc không tồn tại tập tin cấu hình " + filename, "LỖI", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return doc;
        }

        public static void XMLWriter(String filename, String servername, String database, String status)
        {
            try
            {
                XmlTextWriter writer = new XmlTextWriter(filename, null);
                writer.Formatting = Formatting.Indented;

                writer.WriteStartDocument();
                writer.WriteComment("Khong duoc thay doi noi dung file nay."
                                    + "\n Thong so co ban:"
                                    + "\n\t status = true : Quyen Windows."
                                    + "\n\t status = false : Quyen SQL Server."
                                    + "\n\t servername : ten Server."
                                    + "\n\t username : ten dang nhap he thong."
                                    + "\n\t password : mat khau dang nhap he thong."
                                    + "\n\t database : ten co so du lieu.");
                writer.WriteStartElement("config");

                writer.WriteStartElement("status");
                writer.WriteString(status);
                writer.WriteEndElement();

                writer.WriteStartElement("servername");
                writer.WriteString(servername);
                writer.WriteEndElement();

                writer.WriteStartElement("username");
                writer.WriteString("");
                writer.WriteEndElement();

                writer.WriteStartElement("password");
                writer.WriteString("");
                writer.WriteEndElement();

                writer.WriteStartElement("database");
                writer.WriteString(database);
                writer.WriteEndElement();

                writer.WriteEndElement();
                writer.WriteEndDocument();

                writer.Close();
            }
            catch
            {

            }
        }


        public static void XMLWriter(String filename, String servername, String username, String password, String database, String status)
        {
            try
            {
                XmlTextWriter writer = new XmlTextWriter(filename, null);
                writer.Formatting = Formatting.Indented;

                writer.WriteStartDocument();
                writer.WriteComment("Khong duoc thay doi noi dung file nay."
                                    + "\n Thong so co ban:"
                                    + "\n\t status = true : Quyen Windows."
                                    + "\n\t status = false : Quyen SQL Server."
                                    + "\n\t servername : ten Server."
                                    + "\n\t username : ten dang nhap he thong."
                                    + "\n\t password : mat khau dang nhap he thong."
                                    + "\n\t database : ten co so du lieu.");
                writer.WriteStartElement("config");

                writer.WriteStartElement("status");
                writer.WriteString(status);
                writer.WriteEndElement();

                writer.WriteStartElement("servername");
                writer.WriteString(servername);
                writer.WriteEndElement();

                writer.WriteStartElement("username");
                writer.WriteString(username);
                writer.WriteEndElement();

                writer.WriteStartElement("password");
                writer.WriteString(password);
                writer.WriteEndElement();

                writer.WriteStartElement("database");
                writer.WriteString(database);
                writer.WriteEndElement();

                writer.WriteEndElement();
                writer.WriteEndDocument();

                writer.Close();
            }
            catch
            {

            }
        }
    }
}
